package com.mt.api.modules.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.common.utils.PloverDateUtil;
import com.mt.api.constants.enums.OutStoreStatusEnum;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.domain.AppOrder;
import com.mt.api.modules.domain.OutStore;
import com.mt.api.modules.domain.OutStoreDetail;
import com.mt.api.modules.domain.bo.AppOutStorePageBo;
import com.mt.api.modules.domain.vo.AppOrderSimpleVo;
import com.mt.api.modules.domain.vo.OutStoreDetailVo;
import com.mt.api.modules.domain.vo.OutStoreSimpleVo;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.modules.manager.IOrderManager;
import com.mt.api.modules.manager.IOutStoreDetailManager;
import com.mt.api.modules.manager.IOutStoreManager;
import com.mt.api.modules.mapstruct.AppOrderMapStruct;
import com.mt.api.modules.mapstruct.OutStoreMapStruct;
import com.mt.api.modules.service.IOutStoreDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: plover
 * @Date: 2022-05-30
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class OutStoreDetailServiceImpl implements IOutStoreDetailService {


    private final IOutStoreManager outStoreManager;
    private final IOutStoreDetailManager outStoreDetailManager;
    private final IOrderManager orderManager;
    private final ICargoManager cargoManager;

    private final OutStoreMapStruct outStoreMapStruct;
    private final AppOrderMapStruct orderMapStruct;


    @Override
    public WebResponse getOutStorePage(AppOutStorePageBo bo) {
        Page page = outStoreManager.getOutStorePage(bo);
        List<OutStore> records = page.getRecords();
        List<OutStoreSimpleVo> list = records.stream().map(outStore -> {
            OutStoreSimpleVo vo = outStoreMapStruct.toVo(outStore);
            vo.setOutStoreTime(PloverDateUtil.dateToDisplayTime(outStore.getOutStoreTime()));
            vo.setStatusText(OutStoreStatusEnum.nameOfCode(vo.getStatus()));
            return vo;
        }).collect(Collectors.toList());
        page.setRecords(list);
        return WebResponseUtils.successPage(page);
    }

    @Override
    public WebResponse<OutStoreDetailVo> getOutStoreDetail(Integer outStoreId) {
        OutStore outStore = outStoreManager.getById(outStoreId);
        if (Objects.isNull(outStore)) {
            return WebResponseUtils.error("出库单不存在");
        }
        OutStoreDetailVo vo = new OutStoreDetailVo();
        OutStoreSimpleVo outStoreSimpleVo = outStoreMapStruct.toVo(outStore);
        vo.setOutStore(outStoreSimpleVo);

        Integer orderId = outStore.getOrderId();
        AppOrder order = orderManager.getById(orderId);
        AppOrderSimpleVo orderSimpleVo = orderMapStruct.entityToSimpleVo(order);
        vo.setOrder(orderSimpleVo);

        //出库单详细信息
        List<OutStoreDetail> outStoreDetailList = outStoreDetailManager.getByOutStoreId(outStoreId);
        List<OutStoreDetailVo.Detail> details = Lists.newArrayList();

        List<Integer> cargoIds = outStoreDetailList.stream().map(OutStoreDetail::getCargoId).distinct().collect(Collectors.toList());
        List<AppCargo> cargoList = cargoManager.listByIds(cargoIds);

        Map<Integer, List<OutStoreDetail>> cargoMap = outStoreDetailList.stream().collect(Collectors.groupingBy(OutStoreDetail::getCargoId));
        cargoMap.forEach((cargoId, outStoreDetails) -> {
            OutStoreDetailVo.Detail detail = new OutStoreDetailVo.Detail();

            Optional<AppCargo> cargoOptional = cargoList.stream().filter(o -> Objects.equals(o.getId(), cargoId)).findFirst();
            if (cargoOptional.isPresent()) {
                AppCargo appCargo = cargoOptional.get();
                detail.setCargoNo(appCargo.getCargoNo());
                detail.setCargoName(appCargo.getRemark());
                detail.setPicUrl(appCargo.getPicUrl());
            }
            detail.setCargoId(cargoId);

            List<OutStoreDetailVo.SkuInfo> simpleVos = Lists.newArrayList();
            for (OutStoreDetail outStoreDetail : outStoreDetails) {
                OutStoreDetailVo.SkuInfo simpleDetailVo = new OutStoreDetailVo.SkuInfo();
                simpleDetailVo.setId(outStoreDetail.getId());
                simpleDetailVo.setColor(outStoreDetail.getColor());
                simpleDetailVo.setSize(outStoreDetail.getSize());
                simpleDetailVo.setCargoNo(outStoreDetail.getCargoNo());
                simpleDetailVo.setQty(outStoreDetail.getQty());
                simpleVos.add(simpleDetailVo);
            }
            detail.setSkuInfoList(simpleVos);
            details.add(detail);
        });
        List<OutStoreDetailVo.Detail> detailList = details.stream().sorted(Comparator.comparing(OutStoreDetailVo.Detail::getCargoNo)).collect(Collectors.toList());
        vo.setDetails(detailList);
        return WebResponseUtils.success(vo);
    }
}
